package com.wu.project.admin.service.impl;

import cn.hutool.core.date.format.FastDateFormat;
import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.dao.VehicleDao;
import com.wu.project.admin.entity.PageData;
import com.wu.project.admin.entity.VehicleClassifyDTO;
import com.wu.project.admin.service.VehicleService;
import com.wu.project.admin.util.CommonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xuan
 */
@Service
public class VehicleServiceImpl implements VehicleService {

    private final FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    @Autowired
    VehicleDao vehicleDao;

    @Override
    public JsonResult<PageData> queryList(VehicleClassifyDTO vehicleClassifyDTO) {
        Integer offset = CommonResultUtil.Instance.getOffset(vehicleClassifyDTO.getPageNum(), vehicleClassifyDTO.getPageSize());
        vehicleClassifyDTO.setOffset(offset);

        List<VehicleClassifyDTO> vehicleList = vehicleDao.queryVehicleList(vehicleClassifyDTO);
        Integer total = vehicleDao.queryVehicleCount(vehicleClassifyDTO);
        PageData pageData = new PageData();
        pageData.setPageNum(vehicleClassifyDTO.getPageNum());
        pageData.setPageSize(vehicleClassifyDTO.getPageSize());
        pageData.setTotal(total);
        pageData.setData(vehicleList);
        return CommonResultUtil.Instance.success(pageData);
    }

    @Override
    public JsonResult addVehicle(VehicleClassifyDTO vehicleClassifyDTO) {
        String format = fastDateFormat.format(new Date());
        vehicleClassifyDTO.setVehicleClassifyCreatime(format);
        Integer integer = vehicleDao.addVehicle(vehicleClassifyDTO);
        if(integer > 0){
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult updateVehicle(VehicleClassifyDTO vehicleClassifyDTO) {
        Integer integer = vehicleDao.updateVehicle(vehicleClassifyDTO);
        if (integer > 0){
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult deleteVehicle(VehicleClassifyDTO vehicleClassifyDTO) {
        Integer integer = vehicleDao.deleteVehicle(vehicleClassifyDTO);
        if (integer > 0){
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }
}
